home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / SAT / HeartQuest sample ƒ / sPlayer.p < prev    next >
Encoding:
Text File  |  1994-07-26  |  4.3 KB  |  178 lines  |  [TEXT/PJMM]

  1. {===============================================}
  2. {================= Player sprite unit ================}
  3. {===============================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { This file is the first of several sprite units, units that holds the full}
  10. {description of the objects to be animated. }
  11.  
  12. unit sPlayer;
  13.  
  14. { Sprite unit. A sprite unit should include the following routines:}
  15. { Init-procedure, that initializes private bitmaps}
  16. { Setup-procedure, that sets variables other than the standard ones set by MakeSprite }
  17. { Handle-procedure, to be called once per iteration until the sprite dies }
  18. { Hittask-procedure (optional), for advanced collission handling. }
  19.  
  20. { This is the sprite unit for the player object, in this case a butterfly. }
  21.  
  22. interface
  23.  
  24.     uses
  25.         GameGlobals, SAT, sHeart, sFlypaper, scores, SoundConst;
  26.  
  27.     var
  28.         stillRunning: boolean;
  29.  
  30.     procedure InitPlayer;
  31.     procedure SetupPlayer (player: SpritePtr);
  32.     procedure HandlePlayer (me: SpritePtr);
  33.     procedure HitPlayer (me, him: SpritePtr);
  34.  
  35. implementation
  36.  
  37.     const
  38. {playerspeed = 16; { Might become a variable one day }
  39.         shift = 4;
  40.  
  41.     var
  42.         playerFace: array[0..3] of FacePtr;
  43.         posh, posv: longint;
  44.  
  45.     procedure InitPlayer;
  46.         var
  47.             ii: integer;
  48.     begin
  49.         for ii := 0 to 3 do
  50.             begin
  51.                 playerFace[ii] := GetFace(140 + ii);
  52.             end;
  53.     end;
  54.  
  55.     procedure SetupPlayer (player: SpritePtr);
  56.     begin
  57.         player^.face := playerFace[0];
  58.         SetRect(player^.hotRect, -15 + 16, -25 + 32, 15 + 16, 0 + 32);
  59.         posh := bsl(player^.position.h, shift);
  60.         posv := bsl(player^.position.v, shift);
  61.         slowcount := 0;
  62.         player^.task := @HandlePlayer;
  63.         player^.hittask := @HitPlayer;
  64.     end;
  65.  
  66.  
  67.     procedure HandlePlayer (me: SpritePtr);
  68.         var
  69.             pt: point;
  70.     begin
  71.         if me^.kind <> 2 then
  72.             me^.kind := 2;
  73.  
  74.         if slowcount > 0 then
  75.             begin
  76.                 slowcount := pred(slowcount);
  77.                 me^.speed := Point(0);
  78. {SetMouse(Point($00AB0100));???? {256,171}
  79.             end
  80.         else
  81.             begin
  82. {Note about the controls:}
  83. {(posh,posv) is the position scaled up by 16 (2**shift)}
  84. {This is my way to implement fixed-point arithmetics efficiently.}
  85. {If we used only integers, we wouldn't get the smooth movement the}
  86. {bufferfly has.}
  87.                 SetPort(gSAT.wind);
  88.                 GetMouse(pt);
  89.  
  90.                 if pt.h < 0 then
  91.                     pt.h := 0;
  92.                 if pt.h > 512 then
  93.                     pt.h := 512;
  94.                 if pt.v < 0 then
  95.                     pt.v := 0;
  96.                 if pt.v > 342 then
  97.                     pt.v := 342;
  98.  
  99.                 me^.speed.h := 15 * me^.speed.h div 16 + pt.h - 256;
  100.                 me^.speed.v := 15 * me^.speed.v div 16 + pt.v - 171;
  101.  
  102. {If we rather want the direct velocity-from-position that Crystal Quest has,}
  103. {we can use the following:}
  104. {me^.speed.h := pt.h - 256;}
  105. {me^.speed.v := pt.v - 171;}
  106.  
  107.                 SetMouse(Point($00AB0100));{256,171}
  108.  
  109.                 posh := posh + me^.speed.h;
  110.                 posv := posv + me^.speed.v;
  111.  
  112.                 me^.position.h := bsr(posh, shift);
  113.                 me^.position.v := bsr(posv, shift);
  114.  
  115.                 if me^.position.h > gSAT.offSizeH - xsize then
  116.                     begin
  117.                         me^.position.h := gSAT.offSizeH - xsize; { ev. xsize - 10? }
  118.                         posh := bsl(gSAT.offSizeH - xsize, shift);
  119.                     end;
  120.                 if me^.position.h < 0 then
  121.                     begin
  122.                         me^.position.h := 0;
  123.                         posh := 0;
  124.                     end;
  125.                 if me^.position.v > gSAT.offSizeV - 32 then
  126.                     begin
  127.                         me^.position.v := gSAT.offSizeV - 32; { ev. xsize - 10? }
  128.                         posv := bsl((gSAT.offSizeV - 32), shift);
  129.                     end;
  130.                 if me^.position.v < 0 then
  131.                     begin
  132.                         me^.position.v := 0;
  133.                         posv := 0;
  134.                     end;
  135.             end; { if slowcount... }
  136.  
  137.         me^.mode := me^.mode + 1;
  138.         if slowcount = 0 then
  139.             begin
  140.                 if me^.mode > 6 then
  141.                     begin
  142.                         me^.mode := 0;
  143.                     end;
  144.                 me^.face := playerFace[abs(me^.mode - 3)];
  145.             end
  146.         else
  147.             begin
  148.                 me^.face := playerFace[band(slowcount, 1) * 3];
  149.                 if slowcount = 1 then
  150.                     SATSoundPlay(Splatt3SndH, 1, true);
  151.             end;
  152.         if bonus > 0 then
  153.             begin
  154.                 bonus := bonus - 1;
  155.                 if bonus mod 10 = 0 then
  156.                     begin
  157.                         addscore(0);
  158.                         if features^^.macho then
  159.                             if bonus < 110 then
  160.                                 SATSoundPlay(TickSndH, 0, false);
  161.                     end
  162.                 else if features^^.macho then
  163.                     if bonus < 26 then
  164.                         if bonus mod 5 = 0 then
  165.                             SATSoundPlay(TickSndH, 0, false);
  166.             end
  167.         else if features^^.macho then
  168.             stillrunning := false;
  169.  
  170.         playerPos := me^.position;
  171.     end;
  172.  
  173.     procedure HitPlayer (me, him: SpritePtr);
  174.     begin
  175. {This would be an alternative place to handle collisions. Not used in this program.}
  176.     end;
  177.  
  178. end.